package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.utils.DateUtils;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizUserTradeRecord;
import cn.sfrog.project.biz.admin.domain.vo.BizUserTradeRecordFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserTradeRecordQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserTradeRecordVO;
import cn.sfrog.project.biz.admin.mapper.BizUserTradeRecordMapper;
import cn.sfrog.project.biz.admin.service.IBizUserTradeRecordService;
import cn.sfrog.project.biz.web.vo.account.AppUserTradeRecordQueryVO;
import cn.sfrog.project.biz.web.vo.account.AppUserTradeRecordResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 用户交易记录Service业务层处理
 */
@Service
public class BizUserTradeRecordServiceImpl extends ServiceImpl<BizUserTradeRecordMapper, BizUserTradeRecord> implements IBizUserTradeRecordService {
    @Resource
    private BizUserTradeRecordMapper bizUserTradeRecordMapper;

    /**
     * 查询用户交易记录
     *
     * @param id 用户交易记录主键
     * @return 用户交易记录
     */
    @Override
    public BizUserTradeRecordVO selectBizUserTradeRecordById(Long id) {
        return bizUserTradeRecordMapper.selectBizUserTradeRecordById(id);
    }

    /**
     * 查询用户交易记录列表
     *
     * @param query 用户交易记录
     * @return 用户交易记录
     */
    @Override
    public List<BizUserTradeRecordVO> selectBizUserTradeRecordList(BizUserTradeRecordQueryVO query) {
        return bizUserTradeRecordMapper.selectBizUserTradeRecordList(query);
    }

    /**
     * 新增用户交易记录
     *
     * @param form 用户交易记录
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizUserTradeRecord(BizUserTradeRecordFormVO form) {
        BizUserTradeRecord bizUserTradeRecord = new BizUserTradeRecord();
        BeanUtils.copyProperties(form, bizUserTradeRecord);
        bizUserTradeRecord.setCreateTime(new Date());
        bizUserTradeRecord.setCreateBy(SecurityUtils.getUsername());
        bizUserTradeRecord.setUpdateTime(new Date());
        bizUserTradeRecord.setUpdateBy(SecurityUtils.getUsername());
        return bizUserTradeRecord.insert();
    }

    /**
     * 修改用户交易记录
     *
     * @param form 用户交易记录
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizUserTradeRecord(BizUserTradeRecordFormVO form) {
        BizUserTradeRecord bizUserTradeRecord = new BizUserTradeRecord();
        BeanUtils.copyProperties(form, bizUserTradeRecord);
        bizUserTradeRecord.setUpdateTime(new Date());
        bizUserTradeRecord.setUpdateBy(SecurityUtils.getUsername());
        return bizUserTradeRecord.updateById();
    }

    /**
     * 批量删除用户交易记录
     *
     * @param ids 需要删除的用户交易记录主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizUserTradeRecordByIds(Long[] ids) {
            return BizUserTradeRecord.DAO.delete(Wrappers.lambdaQuery(BizUserTradeRecord.class).in(BizUserTradeRecord::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
       // return BizUserTradeRecord.DAO.update(Wrappers.lambdaUpdate(BizUserTradeRecord.class).in(BizUserTradeRecord::getId, (Object[]) vo.getIds()).set(BizUserTradeRecord::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    public Boolean saveUserTradeRecord(Long userId, BigDecimal amount, String type,String serialNumber,String remark) {
        BizUserTradeRecord tradeRecord = new BizUserTradeRecord();
        tradeRecord.setUserId(userId);
        tradeRecord.setType(type);
        tradeRecord.setTradeAmount(amount);
        tradeRecord.setSerialNumber(serialNumber);
        tradeRecord.setTradeTime(DateUtils.getNowDate());
        tradeRecord.setRemark(remark);
        tradeRecord.setCreateTime(DateUtils.getNowDate());
        tradeRecord.setCreateBy(userId.toString());
        tradeRecord.setUpdateTime(DateUtils.getNowDate());
        tradeRecord.setUpdateBy(userId.toString());
        return  bizUserTradeRecordMapper.insert(tradeRecord) > 0;
    }

    @Override
    public List<AppUserTradeRecordResultVO> selectAppUserTradeRecordList(AppUserTradeRecordQueryVO queryVO) {
        if(StringUtils.isNotBlank(queryVO.getTradeDate())){
            queryVO.setTradeDate(queryVO.getTradeDate()+ "-01 00:00:00");
        }
        return bizUserTradeRecordMapper.selectAppUserTradeRecordList(queryVO);
    }

    @Override
    public List<AppUserTradeRecordResultVO> selectAppEarnestMoneyList(AppUserTradeRecordQueryVO queryVO) {
        if(StringUtils.isNotBlank(queryVO.getTradeDate())){
            queryVO.setTradeDate(queryVO.getTradeDate()+ "-01 00:00:00");
        }
        return bizUserTradeRecordMapper.selectAppEarnestMoneyList(queryVO);
    }
}
